import ic_bidlogin from "@root/P/images/ic_bidlogin.png";
import showLoginModal from "@root/P/pages/LoginModal/showLoginModal";
import ic_noauth from "@root/P/images/ic_noauth.png";
import ic_givenbid from "@root/P/images/ic_givenbid.png";
import InputBid from "@root/P/pages/BidDetailInfo/InputBid/InputBid";
import Filter from "@module/Filter";
import usePartState from "@module/usePartState";
import useForm from "@module/useForm";


export default function BottomSection(props) {
    const routerParams=$state.params||{};
    let isEdit=routerParams.isEdit;
    const [stateObj, updateState] = usePartState({
        isEditPage:isEdit?true:false,
        isSelected:false,
        bidInfo:{},
        defaultPrice:''
        // refreshFormdata:false
    });
    const {giveupBid}=props;
    const info=props.info||{};
    const {unit,isEnd,classification,isLogin,isSupp,status,bid}=info;

    function toEditPrice() {
        let href=location.href;
        location.href=href+'&isEdit=1'
        updateState({
            isEditPage:true
        })
    }

    function chooseGoods(bidInfo) {
        updateState({
            isSelected:true,
            bidInfo:bidInfo,
        })
        setDefaultPrice(bidInfo)
    }



    function setDefaultPrice(bidInfo) {

        let price=parseInt(bidInfo.price)||0;
        let pattern = /^[0-9]*$/;
        let countEle=document.querySelector('.bid-inputbid-inputs-input input[name=count]')
        // console.log(countEle)
        let count=countEle.value;
        // console.log(count)

        if(pattern.test(count)){
            let totalPrice=(count-0)*(price-0);
            if(totalPrice&&totalPrice>0){
                updateState({
                    defaultPrice:Filter.float(totalPrice,2),
                })
            }else{
                updateState({
                    defaultPrice:'',
                })
            }
        }
    }


    //选择商品，或者重新选择商品
    if(stateObj.isSelected||stateObj.isEditPage){
        //重新选择商品，拿选择商品的信息
        return (
            <div>
                <InputBid
                    isEditPage={stateObj.isEditPage}
                    isSelected={stateObj.isSelected}
                    info={info}
                    bidInfo={stateObj.bidInfo}
                    choose={chooseGoods}
                    defaultPrice={stateObj.defaultPrice}
                />
            </div>
        )
    }


    //已经报过价了，现在重新选择
    if(stateObj.isEditPage){
        return (
            <div>
                <InputBid
                    isEditPage={stateObj.isEditPage}
                    isSelected={stateObj.isSelected}
                    info={info}
                    bidInfo={stateObj.bidInfo}
                    choose={chooseGoods}
                    defaultPrice={stateObj.defaultPrice}
                    // refreshFormdata={stateObj.refreshFormdata}
                />
            </div>
        )
    }


    // //以下为，默认进来页面，根据不同的状态，显示不同的内容
    // return (
    //     <div>
    //         <InputBid
    //             isEditPage={stateObj.isEditPage}
    //             info={info}
    //             choose={chooseGoods}
    //         />
    //     </div>
    // )



    if(!isLogin){
        return (
            <div className={'bid-info-b-nologin'}>
                <img src={ic_bidlogin} alt=""/>
                <div className={'zen_fs-16 zen_646464'}>
                    <span>若想报价或查看已报价，请</span>
                    <span
                        onClick={()=>{
                            showLoginModal('',true)
                        }}
                        className={'zen_0093EB zen_cur-p'}>登录</span>
                </div>
            </div>
        )
    }
    if(!isSupp){
        return (
            <TipsTxt
                img={ic_noauth}
                txt={'您的账户没有竞价权限'}
            />
        )
    }

    if(status==1){  //
        return (
            <TipsTxt
                img={ic_givenbid}
                txt={'您已放弃竞价'}
            />
        )
    }

    if(status==3){  //没参与，还没有填写
        if(isEnd){
            return (
                <TipsTxt
                    img={ic_givenbid}
                    txt={'您没有参与竞价'}
                />
            )
        }
        return (
            <InputBid
                info={info}
                choose={chooseGoods}
            />
        )
    }

    if(status==0||status==2){    //已填写
        if(isEnd){
            return (
                <BidTable
                    info={info}
                />
            )
        }
        return (
            <div>
                <BidTable
                    info={info}
                />
                <TwoBtns
                    clickLeft={toEditPrice}
                    clickRight={giveupBid}
                />
            </div>
        )
    }

    return (
        <TipsTxt
            img={ic_givenbid}
            txt={'您已放弃竞价'}
        />
    )
}



function TwoBtns(props) {
    const {clickLeft,clickRight}=props;
    return(
        <div className={'bid-detail-b-price-btns'}>
            <div
                onClick={clickLeft}
                className={'bid-detail-b-price-yes'}>
                修改报价
            </div>
            <div
                onClick={clickRight}
                className={'bid-detail-b-price-no'}>
                放弃竞价
            </div>
        </div>
    )
}



function BidTable(props) {

    const {classification,bid}=props.info||{}
    const {itemPropertyGroupCode}=props.info.item||{}

    const { attachmentUrl,bidPrice,brandName,code,count, deliveryTime,name,productPrice,remark,sn,spec}=bid||{}

    return(
        <div className={'zen_m-t-20 bid-info-table'}>
            <div className={'zen_flex'}>
                <span className={'bid-info-row-name'}>商品名称</span>
                <span className={'bid-info-row-val'}>{name}</span>
            </div>

            <div className={'zen_flex'}>
                <span className={'bid-info-row-name'}>商品货号</span>
                <span className={'bid-info-row-val'}>{code}</span>
            </div>

            <div className={'zen_flex'}>
                <span className={'bid-info-row-name'}>商品规格</span>
                <span className={'bid-info-row-val'}>{spec}</span>
            </div>

            <div className={'zen_flex'}>
                <span className={'bid-info-row-name'}>商品价格</span>
                <span className={'bid-info-row-val'}>{Filter.float(productPrice,2)}</span>
            </div>

            <div className={'zen_flex'}>
                <span className={'bid-info-row-name'}>商品数量</span>
                <span className={'bid-info-row-val'}>{count}</span>
            </div>

            <div className={'zen_flex'}>
                <span className={'bid-info-row-name'}>商品报价</span>
                <span className={'bid-info-row-val'}>{Filter.float(bidPrice,2)}</span>
            </div>

            <div className={'zen_flex'}>
                <span className={'bid-info-row-name'}>{classification ==4 && itemPropertyGroupCode=="SEQUENCING"? '服务周期':'发货天数'}</span>
                <span className={'bid-info-row-val'}>{deliveryTime}</span>
            </div>

            <div className={'zen_flex'}>
                <span className={'bid-info-row-name'}>文件附件</span>
                {
                    attachmentUrl?
                        <div className={'bid-info-row-download'}>
                           <span
                               onClick={()=>{
                                   window.open(`/bid${attachmentUrl}`)
                               }}
                           >{'下载文件'}</span>
                        </div>
                        :
                        <span className={'bid-info-row-val'}>{'无'}</span>
                }

            </div>

            <div className={'zen_flex'}>
                <span className={'bid-info-row-name'}>我的优势</span>
                <span className={'bid-info-row-val'}>{remark||'-'}</span>
            </div>
        </div>
    )
}



function TipsTxt(props) {
    return (
        <div className={'bid-info-b-nosupp'}>
            <img src={props.img} alt=""/>
            <div className={'zen_fs-14 zen_969696'}>
                <span>{props.txt}</span>
            </div>
        </div>
    )
}